package com.example.android2019.note;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.android2019.R;

import java.util.ArrayList;

public class NoteAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<NoteBean> list;

    public NoteAdapter(Context context, ArrayList<NoteBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        /*return list == null ? 0 : list.size();*/
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            /*第一次：关联布局文件；关联控件*/
            convertView = View.inflate(context, R.layout.activity_note_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_content = convertView.findViewById(R.id.note_item_content);
            viewHolder.tv_time = convertView.findViewById(R.id.note_item_time);
            convertView.setTag(viewHolder);
        } else {
            /*以后：重用*/
            viewHolder = (ViewHolder) convertView.getTag();
        }
        /*设置控件内容*/
        NoteBean noteBean = list.get(position);
        viewHolder.tv_content.setText(noteBean.getContent());
        viewHolder.tv_time.setText(noteBean.getTime());
        return convertView;
    }

    class ViewHolder {
        TextView tv_content;
        TextView tv_time;
    }
}
